﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using LiveTiss.Classes.TypeConverter;
using LiveTiss.Schemas;

namespace LiveTiss.Guias.UserControls
{
    public partial class DenteRegiao : UserControl
    {
        #region Value

        public ct_denteRegiao Value
        {
            get { return (ct_denteRegiao)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(ct_denteRegiao), typeof(DenteRegiao), new PropertyMetadata(null));

        #endregion

        public DenteRegiao()
        {
            this.DataContext = this;
            if (this.Value == null)
            {
                this.Value = new ct_denteRegiao()
                {
                    ItemElementName = ItemChoiceType8.dente,
                    ItemValue = st_dente.Item11
                };
            }
            InitializeComponent();
        }

        public void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs ea)
        {
            var cbx = sender as ComboBox;

            Binding b = new Binding();

            b.Path = new PropertyPath("Value.ItemValue");
            b.ElementName = "ucDenteRegiao";
            b.Mode = BindingMode.TwoWay;
            b.ValidatesOnDataErrors = true;
            b.NotifyOnValidationError = true;
            b.Converter = new EnumConverter();

            switch (cbx.SelectedIndex)
            {
                case 0:
                    b.ConverterParameter = "Dente";

                    this.cbxDente.Visibility = System.Windows.Visibility.Visible;
                    this.cbxRegiao.Visibility = System.Windows.Visibility.Collapsed;

                    this.Value.ItemValue = st_dente.Item11;

                    this.cbxRegiao.ClearValue(ComboBox.SelectedIndexProperty);
                    this.cbxDente.ItemsSource = this.Value.denteItems;
                    this.cbxDente.SetBinding(ComboBox.SelectedIndexProperty, b);

                    break;
                case 1:
                    b.ConverterParameter = "Regiao";

                    this.cbxDente.Visibility = System.Windows.Visibility.Collapsed;
                    this.cbxRegiao.Visibility = System.Windows.Visibility.Visible;

                    this.Value.ItemValue = st_regiao.AI;

                    this.cbxDente.ClearValue(ComboBox.SelectedIndexProperty);
                    //if(this.cbxRegiao.Items.Count > 0)
                    this.cbxRegiao.ItemsSource = this.Value.regiaoItems;
                    this.cbxRegiao.SetBinding(ComboBox.SelectedIndexProperty, b);

                    break;
            }
        }
    }
}
